#!/usr/bin/env python

#############################################################################
# makedb.py: Sample class for creating a new, empty database from scratch.
#############################################################################
# Copyright (C) 2012
#
# Authors: Dan Larkin <danielhlarkin@gmail.com>
#          Yonatan Naamad <cached@gmail.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#############################################################################

import sqlite3

def make_db(db_name):
    s = """BEGIN TRANSACTION;
           CREATE TABLE IF NOT EXISTS permissions (torrent_id TEXT, user_id NUMERIC);
           CREATE TABLE IF NOT EXISTS torrents (hash BLOB, t_max NUMERIC, t_min NUMERIC, size NUMERIC, id INTEGER PRIMARY KEY, path TEXT);
           CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, key TEXT);
           CREATE UNIQUE INDEX IF NOT EXISTS key_lookup ON users(key ASC);
           CREATE INDEX IF NOT EXISTS  torrents_lookup ON permissions(user_id ASC);
           COMMIT;"""
    conn = sqlite3.connect(db_name)
    conn.executescript(s)
    conn.close()
    
if __name__ == '__main__':
    import sys
    if len(sys.argv) != 2:
        print "Usage: ./makedb.py <db_name>"
    else:
        make_db(sys.argv[1])
